<?php
class Easynote_Controller_Home extends Easynote_Controller_Base{
    
    public $user = array();

    public function init(){
        $this->user = get_user();
        //crsf验证
        parent::init();
    }
    
    //首页
    public function actionIndex(){
        //获取笔记本列表
        $model_book = MONK::getSingleton('Easynote_Model_Book');
        $model_book->user_id = $this->user['user_id'];
        $books = $model_book->get_all();
        //获取记录总数,默认记录数,星标记数
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        $all_count = $model_record->get_all_count();
        $default_count = $model_record->get_default_count();
        $star_count = $model_record->get_star_count();
        //获取分页记录
        $records = $model_record->get_page();
        $page_count = ceil($all_count*0.1);
        
        $this->assign('page',1);
        $this->assign('books',$books);
        $this->assign('all_count',$all_count);
        $this->assign('default_count',$default_count);
        $this->assign('star_count',$star_count);
        $this->assign('records',$records);
        $this->assign('page_count',$page_count);
        $this->render();
    }
    
    //获取记事内容
    public function actionRecord_AJAX(){
        $this->_setType(array('record_id'=>PARAM_STRING));
        $record_id = $this->_get('record_id');
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        $record = $model_record->get_one($record_id);
        return $this->json($record);
    }
   
    //保存记事
    public function actionSaveRecord_AJAX_POST(){
        $this->_setType(array('id'=>PARAM_STRING,'title'=>PARAM_STRING,'geo_address'=>PARAM_STRING,'content'=>array('func'=>PARAM_STRING,'argv'=>PARAM_TEXT),'book_id'=>PARAM_STRING,'action'=>PARAM_STRING),'post');
        $action = $this->_post('action');
        $id = $this->_post('id');
        $title = $this->_post('title');
        $content = $this->_post('content');
        $book_id = $this->_post('book_id');
        $geo_address = $this->_post('geo_address');
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        $result = array();
        $result['action'] = $action;
        $result['title'] = $title;
        $result['book_id'] = $book_id;
        $result['geo_address'] = $geo_address;
        $result['updated'] = time();
        $result['title'] = $title;
        if($action == 'create'){
            $result['id'] = $model_record->add(array('book_id'=>$book_id, 'title'=>$title, 'content'=>$content, 'geo_address'=>$geo_address));
        }else{
            $result['id'] = $id;
            $result['result'] = $model_record->edit($id, array('title'=>$title, 'content'=>$content, 'geo_address'=>$geo_address));
        }
        
        return $this->json($result);
    }
    
    //更改记事本ID
    public function actionChangeRecordBook_AJAX_POST(){
        $this->_setType(array('id'=>PARAM_STRING,'book_id'=>PARAM_STRING),'post');
        $id = $this->_post('id');
        if(empty($id)) return false;
        $book_id = $this->_post('book_id');
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        $result = $model_record->update_book_id($id, $book_id);
        return $this->json($result);
    }
    
    //通过book获取列表
    public function actionList_AJAX(){
        $this->_setType(array('page'=>PARAM_UINT, 'book_id'=>PARAM_STRING));
        $page = $this->_get('page');
        $book_id = $this->_get('book_id');
        if(empty($book_id)) $book_id = 'all';
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        //可改进：直接根据JS获取book后面的数量来进行分页
        if($book_id == 'all'){
            $count = ceil($model_record->get_all_count()/10);
            $records = $model_record->get_page($page);
        }elseif($book_id == 'default'){
            $count = ceil($model_record->get_default_count()/10);
            $records = $model_record->get_page_default($page);
        }else{
            $count = ceil($model_record->get_count_by_book_id($book_id)/10);
            $records = $model_record->get_page_by_book_id($book_id,$page);
        }
        return $this->json(array('pageCount'=>$count,'records'=>$records));
    }

    //获取星标记列表
    public function actionHasStar_AJAX(){
        $this->_setType(array('page'=>PARAM_UINT));
        $page = $this->_get('page');
        $model_record = MONK::getSingleton('Easynote_Model_Record');
        $model_record->user_id = $this->user['user_id'];
        $count = ceil($model_record->get_star_count()/10);
        $records = $model_record->get_page_star($page);
        return $this->json(array('pageCount'=>$count,'records'=>$records));
    }

    //存储book
    public function actionSaveBook_AJAX(){
        $this->_setType(array('book_id'=>PARAM_STRING, 'name'=>PARAM_STRING));
        $book_id = $this->_get('book_id');
        $name = $this->_get('name');
        $model_book = MONK::getSingleton('Easynote_Model_Book');
        $model_book->user_id = $this->user['user_id'];
        if(empty($name)){
            $return = false;
        }elseif(empty($book_id)){
            $return = $model_book->add($name);
        }else{
            $return = $model_book->edit($book_id,$name);
        }
        return $this->json($return);
    }
}
